<?php

use app\models\CardAttr;
use app\models\CardAttrValue;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;

/**
 * @var $this yii\web\View
 * @var $model app\models\BatchSetAttrForm
 */

$this->title = '批量设置卡属性';
$this->params['breadcrumbs'][] = '物联网卡管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<?php $form = ActiveForm::begin();?>
<fieldset>
    <legend><label><input type="radio" name="BatchSetAttrForm[source]" value="range" checked="checked"> 设定起止卡号</label></legend>
    <?php echo $form->field($model, 'iccid_start')->hint('开始号码和结束号码必须同时填写。');?>
    <?php echo $form->field($model, 'iccid_end')->hint('开始号码结束号码和ICCID列表必须至少填写一项。');?>
</fieldset>
<?php echo $form->field($model, 'iccid_list')->textarea(['style' => 'min-height:200px;'])->label('<label><input type="radio" name="BatchSetAttrForm[source]" value="iccid"> 直接填写ICCID列表</label>');?>
<fieldset>
    <legend>设置属性</legend>
    <?php /** @var CardAttr $attr */
    foreach (CardAttr::find()->andWhere(['uid' => Yii::$app->user->id, 'status' => CardAttr::STATUS_OK])->orderBy('sort DESC, id ASC')->each() as $attr) {
        $attrValue = new CardAttrValue();?>
        <div class="form-group required">
            <label class="control-label" for="card-attr-<?php echo $attr->id;?>"><?php echo Html::encode($attr->name);?></label>
            <?php switch ($attr->type) {
                case CardAttr::TYPE_PASSWORD:
                    echo Html::activePasswordInput($attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'class' => 'form-control', 'name' => 'CardAttrValue_' . $attr->id]);
                    break;
                case CardAttr::TYPE_TEXT:
                case CardAttr::TYPE_URL:
                    echo Html::activeTextInput($attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'class' => 'form-control', 'name' => 'CardAttrValue_' . $attr->id]);
                    break;
                case CardAttr::TYPE_MULTI_TEXT:
                case CardAttr::TYPE_RICH_TEXT:
                    echo Html::activeTextarea($attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'class' => 'form-control', 'name' => 'CardAttrValue_' . $attr->id]);
                    break;
                case CardAttr::TYPE_NUMBER:
                    echo Html::activeInput('number', $attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'class' => 'form-control', 'name' => 'CardAttrValue_' . $attr->id]);
                    break;
                case CardAttr::TYPE_EMAIL:
                    echo Html::activeInput('email', $attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'class' => 'form-control', 'name' => 'CardAttrValue_' . $attr->id]);
                    break;
                case CardAttr::TYPE_RADIO:
                    $options = preg_split('/\r|\n|\r\n/', $attr->options, -1, PREG_SPLIT_NO_EMPTY);
                    $attrValue->value = preg_split('/\r|\n|\r\n/', $attrValue->value, -1, PREG_SPLIT_NO_EMPTY);
                    echo Html::activeRadioList($attrValue, 'value', array_combine($options, $options), ['name' => 'CardAttrValue_' . $attr->id]);
                    break;
                case CardAttr::TYPE_CHECKBOX:
                    $options = preg_split('/\r|\n|\r\n/', $attr->options, -1, PREG_SPLIT_NO_EMPTY);
                    $attrValue->value = preg_split('/\r|\n|\r\n/', $attrValue->value, -1, PREG_SPLIT_NO_EMPTY);
                    echo Html::activeCheckboxList($attrValue, 'value', array_combine($options, $options), ['name' => 'CardAttrValue_' . $attr->id]);
                    break;
                case CardAttr::TYPE_DATETIME:
                    echo Html::activeTextInput($attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'class' => 'form-control masked', 'data-mask'=>'9999-99-99 99:99:99', 'name' => 'CardAttrValue_' . $attr->id]);
                    break;
                case CardAttr::TYPE_DATE:
                    echo Html::activeTextInput($attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'class' => 'form-control masked', 'data-mask'=>'9999-99-99', 'name' => 'CardAttrValue_' . $attr->id]);
                    break;
                case CardAttr::TYPE_TIME:
                    echo Html::activeTextInput($attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'class' => 'form-control masked', 'data-mask'=>'99:99:99', 'name' => 'CardAttrValue_' . $attr->id]);
                    break;
                case CardAttr::TYPE_FILE:
                    echo Html::activeFileInput($attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'name' => 'CardAttrValue_' . $attr->id]);
                    break;
                case CardAttr::TYPE_IMAGE:
                    echo Html::activeFileInput($attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'name' => 'CardAttrValue_' . $attr->id]);
                    break;
                case CardAttr::TYPE_COLOR:
                    echo Html::activeTextInput($attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'class' => 'form-control', 'name' => 'CardAttrValue_' . $attr->id]);
                    break;
                default:
                    echo Html::activeTextInput($attrValue, 'value', ['id' => 'card-attr-' . $attr->id, 'class' => 'form-control', 'name' => 'CardAttrValue_' . $attr->id]);
            }?>
            <div class="help-block"><?php echo Html::encode($attr->desc);?></div>
        </div>
    <?php }?>
</fieldset>
<div class="form-group">
    <div class="col-lg-offset-1 col-lg-11">
        <button type="button" class="btn btn-default" onclick="window.history.go(-1);"><i class="ace-icon fa fa-arrow-left bigger-110"></i>返回</button>
        <button class="btn btn-primary"><i class="ace-icon fa fa-check bigger-110"></i>保存</button>
        <button type="reset" class="btn btn-warning"><i class="ace-icon fa fa-undo bigger-110"></i>重置</button>
    </div>
</div>
<?php $form->end();?>
<script>
    function pageInit() {
        $('[name="BatchSetAttrForm[search_api_id]"]').change(function () {
            var id = $(this).val();
            $.pjax.reload('#pjax_isp', {
                history: false,
                push: false,
                url: '<?php echo Url::current(['ajax' => 1]);?>&search_api_id=' + id
            });
        });
    }
</script>
